package com.crm.controller;

import com.crm.dto.LoginDto;
import com.crm.entity.User;
import com.crm.properties.JwtProperties;
import com.crm.result.Result;
import com.crm.service.UserService;
import com.crm.utils.JwtUtil;
import com.google.code.kaptcha.Producer;
import io.netty.handler.codec.base64.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

//登录接口
@RestController
public class UserController {
    @Autowired
    private UserService loginService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto){
        User user = loginService.login(loginDto);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", user.getUserId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);
        return Result.success(token);

    }


    @GetMapping("/captchaImage")
    public Result generateCaptcha() {

        return Result.success();
    }

}
